<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="事故编号" prop="iNumber">
        <el-input
          v-model="queryParams.iNumber"
          placeholder="请输入事故编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发生地点" prop="iSite">
        <el-input
          v-model="queryParams.iSite"
          placeholder="请输入发生地点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发生时间" prop="iDate">
        <el-date-picker clearable
          v-model="queryParams.iDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择发生时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="责任人" prop="iUId">
        <el-input
          v-model="queryParams.iUId"
          placeholder="请输入责任人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['incidentLog:incident:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['incidentLog:incident:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['incidentLog:incident:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['incidentLog:incident:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="incidentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="${comment}" align="center" prop="iId" />
      <el-table-column label="事故编号" align="center" prop="iNumber" />
      <el-table-column label="0-1-2-3 消防事故、工程事故、安全事故。其他事故" align="center" prop="iType" />
      <el-table-column label="发生地点" align="center" prop="iSite" />
      <el-table-column label="发生时间" align="center" prop="iDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.iDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="责任人" align="center" prop="iUId" />
      <el-table-column label="事故描述" align="center" prop="iStatement" />
      <el-table-column label="事故照片" align="center" prop="iImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.iImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['incidentLog:incident:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['incidentLog:incident:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改园区事件记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="事故编号" prop="iNumber">
          <el-input v-model="form.iNumber" placeholder="请输入事故编号" />
        </el-form-item>
        <el-form-item label="发生地点" prop="iSite">
          <el-input v-model="form.iSite" placeholder="请输入发生地点" />
        </el-form-item>
        <el-form-item label="发生时间" prop="iDate">
          <el-date-picker clearable
            v-model="form.iDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发生时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="责任人" prop="iUId">
          <el-input v-model="form.iUId" placeholder="请输入责任人" />
        </el-form-item>
        <el-form-item label="事故描述" prop="iStatement">
          <el-input v-model="form.iStatement" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="事故照片">
          <image-upload v-model="form.iImg"/>
        </el-form-item>
        <el-divider content-position="center">用户信息信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddSysUser">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteSysUser">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="sysUserList" :row-class-name="rowSysUserIndex" @selection-change="handleSysUserSelectionChange" ref="sysUser">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="部门ID" prop="deptId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.deptId" placeholder="请输入部门ID" />
            </template>
          </el-table-column>
          <el-table-column label="用户账号" prop="userName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.userName" placeholder="请输入用户账号" />
            </template>
          </el-table-column>
          <el-table-column label="用户昵称" prop="nickName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.nickName" placeholder="请输入用户昵称" />
            </template>
          </el-table-column>
          <el-table-column label="用户类型" prop="userType" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.userType" placeholder="请选择用户类型">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="用户邮箱" prop="email" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.email" placeholder="请输入用户邮箱" />
            </template>
          </el-table-column>
          <el-table-column label="手机号码" prop="phonenumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.phonenumber" placeholder="请输入手机号码" />
            </template>
          </el-table-column>
          <el-table-column label="用户性别" prop="sex" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.sex" placeholder="请选择用户性别">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="头像地址" prop="avatar" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.avatar" placeholder="请输入头像地址" />
            </template>
          </el-table-column>
          <el-table-column label="密码" prop="password" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.password" placeholder="请输入密码" />
            </template>
          </el-table-column>
          <el-table-column label="帐号状态" prop="status" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.status" placeholder="请选择帐号状态">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="最后登录IP" prop="loginIp" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.loginIp" placeholder="请输入最后登录IP" />
            </template>
          </el-table-column>
          <el-table-column label="最后登录时间" prop="loginDate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.loginDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择最后登录时间" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listIncident, getIncident, delIncident, addIncident, updateIncident } from "@/api/incidentLog/incident";

export default {
  name: "Incident",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedSysUser: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 园区事件记录表格数据
      incidentList: [],
      // 用户信息表格数据
      sysUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        iNumber: null,
        iType: null,
        iSite: null,
        iDate: null,
        iUId: null,
        iStatement: null,
        iImg: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询园区事件记录列表 */
    getList() {
      this.loading = true;
      listIncident(this.queryParams).then(response => {
        this.incidentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        iId: null,
        iNumber: null,
        iType: null,
        iSite: null,
        iDate: null,
        iUId: null,
        iStatement: null,
        iImg: null
      };
      this.sysUserList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.iId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加园区事件记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const iId = row.iId || this.ids
      getIncident(iId).then(response => {
        this.form = response.data;
        this.sysUserList = response.data.sysUserList;
        this.open = true;
        this.title = "修改园区事件记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.sysUserList = this.sysUserList;
          if (this.form.iId != null) {
            updateIncident(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addIncident(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const iIds = row.iId || this.ids;
      this.$modal.confirm('是否确认删除园区事件记录编号为"' + iIds + '"的数据项？').then(function() {
        return delIncident(iIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 用户信息序号 */
    rowSysUserIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 用户信息添加按钮操作 */
    handleAddSysUser() {
      let obj = {};
      obj.deptId = "";
      obj.userName = "";
      obj.nickName = "";
      obj.userType = "";
      obj.email = "";
      obj.phonenumber = "";
      obj.sex = "";
      obj.avatar = "";
      obj.password = "";
      obj.status = "";
      obj.loginIp = "";
      obj.loginDate = "";
      obj.remark = "";
      this.sysUserList.push(obj);
    },
    /** 用户信息删除按钮操作 */
    handleDeleteSysUser() {
      if (this.checkedSysUser.length == 0) {
        this.$modal.msgError("请先选择要删除的用户信息数据");
      } else {
        const sysUserList = this.sysUserList;
        const checkedSysUser = this.checkedSysUser;
        this.sysUserList = sysUserList.filter(function(item) {
          return checkedSysUser.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleSysUserSelectionChange(selection) {
      this.checkedSysUser = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('incidentLog/incident/export', {
        ...this.queryParams
      }, `incident_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
